home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / CHARCLAS.C < prev    next >
Text File  |  1989-12-30  |  1KB  |  42 lines

  1. #include "stdio.h"
  2. #include "ctype.h"  /* Note - your compiler may not need this */
  3.  
  4. main()
  5. {
  6. FILE *fp;
  7. char line[80], filename[24];
  8. char *c;
  9.  
  10.    printf("Enter filename -> ");
  11.    scanf("%s",filename);
  12.    fp = fopen(filename,"r");
  13.  
  14.    do {
  15.       c = fgets(line,80,fp);   /* get a line of text */
  16.       if (c != NULL) {
  17.          count_the_data(line);
  18.       }
  19.    } while (c != NULL);
  20.  
  21.    fclose(fp);
  22. }
  23.  
  24. count_the_data(line)
  25. char line[];
  26. {
  27. int whites, chars, digits;
  28. int index;
  29.  
  30.    whites = chars = digits = 0; 
  31.  
  32.    for (index = 0;line[index] != 0;index++) {
  33.       if (isalpha(line[index]))   /* 1 if line[] is alphabetic  */
  34.           chars++;
  35.       if (isdigit(line[index]))   /* 1 if line[] is a digit     */
  36.           digits++;
  37.       if (isspace(line[index]))   /* 1 if line[] is blank, tab, */
  38.           whites++;               /*           or newline       */ 
  39.    }   /* end of counting loop */
  40.  
  41.    printf("%3d%3d%3d %s",whites,chars,digits,line);
  42. }